package se19team1e.dao.impl.property;

import java.util.ArrayList;
import java.util.List;

import se19team1e.dao.abstrakt.jaas.AccountDAO;
import se19team1e.dao.abstrakt.property.PropertyDAO;
import se19team1e.entity.AccountDTO;
import se19team1e.exception.DataAccessException;

/**
 * Concrete class implementation of the Property DAO, Property DAO is handling
 * account in this implementation.
 * 
 * @author Palaniappan Vairavan
 * 
 */
public class AccountPropertyDAO extends PropertyDAO<AccountDTO> implements
		AccountDAO {

	/**
	 * Constructor class for Account Property DAO will call the super class for
	 * the Account Property
	 * 
	 * @param jobAdvertProperties
	 *            Name of the Account Properties file
	 */
	public AccountPropertyDAO(String jobAdvertProperties) {
		super(jobAdvertProperties);
	}

	/**
	 * Convert DTO into properties given the Account DTO object
	 * 
	 * @return true if create Account DTO is successful)
	 * @throws DataAccessException
	 */
	@Override
	public boolean create(AccountDTO dtoType) throws DataAccessException {

		convertDTOIntoProperties(dtoType);
		persist();
		return true;
	}

	/**
	 * 
	 * DAO will deletes an account given its ID
	 * 
	 * @param id
	 *            of the account object to be deleted
	 * @return true if delete is successful
	 * @throws DataAccessException
	 * 
	 */
	@Override
	public boolean delete(Object id) throws DataAccessException {

		AccountDTO accountDTO = retrieveById(id);
		accountDTO.setDeleted(true);
		convertDTOIntoProperties(accountDTO);
		persist();

		return true;
	}

	/**
	 * DAO will retrieve all account objects in the persistence storage.
	 * 
	 * @return list of account DataTransfer objects
	 * @throws DataAccessException
	 */
	@Override
	public List<AccountDTO> retrieveAll() throws DataAccessException {
		List<AccountDTO> accountDTOList = new ArrayList<AccountDTO>();
		int lastSequence = getLastSequence();
		AccountDTO accountDTO = null;
		for (int currentSequence = 1; currentSequence <= lastSequence; currentSequence++) {
			accountDTO = new AccountDTO();
			populateValuesIntoDTO(String.valueOf(currentSequence), accountDTO);
			accountDTOList.add(accountDTO);

		}
		return accountDTOList;
	}

	/**
	 * DAO will retrieve account objects in the persistence storage based on the
	 * id
	 * 
	 * @param id
	 *            unique id of the object
	 * @return list of account DataTransfer objects
	 * @throws DataAccessException
	 */
	@Override
	public AccountDTO retrieveById(Object id) throws DataAccessException {

		AccountDTO accountDTO = new AccountDTO();
		populateValuesIntoDTO((String) id, accountDTO);
		return accountDTO;
	}

	/**
	 * DAO will update account objects in the persistence storage based 
	 * 
	 * @param dto
	 *            Account DataTransfer Object
	 * @return list of account DataTransfer objects
	 * @throws DataAccessException
	 */
	@Override
	public AccountDTO update(AccountDTO dto) throws DataAccessException {

		convertDTOIntoProperties(dto);
		persist();
		return retrieveById(dto.getId());
	}

	/**
	 * DAO will retrieve account objects in the persistence storage based 
	 * 
	 * @param username
	 *            username of the account (string format)
	 * @return null if nothing is found otherwise account DTO
	 * @throws DataAccessException
	 */
	@Override
	public AccountDTO retrieveByUsername(String username)
			throws DataAccessException {
		List<AccountDTO> allAccounts = retrieveAll();

		if (allAccounts != null && allAccounts.size() > 0) {
			for (AccountDTO accountDTO : allAccounts) {
				if (accountDTO.getUsername().equalsIgnoreCase(username)) {
					return accountDTO;
				}

			}

		}

		return null;
	}

}